package cn.linran.controller;

import cn.linran.domain.Product;
import cn.linran.domain.Store;
import cn.linran.feignclient.Userfeignclient;
import cn.linran.service.IProductService;
import cn.linran.util.jsonResult;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class productController {
    @Autowired
    private IProductService iProductService;

    @Autowired
    private Userfeignclient userfeignclient;
    @GetMapping("findById/{id}")
    @SentinelResource(value = "product-service",blockHandler = "degradationMethod")
    public Product findById(@PathVariable("id")Long id){
       return iProductService.findById(id);
    }

    @PostMapping("/add")
    public jsonResult add(@RequestBody Product product){
        try {
            iProductService.add(product);
            Store store = product.getStore();
            store.setProduct_id(product.getId());
            userfeignclient.add(store);
            return jsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setResultObj("网络拥堵");
        }

    }
    public Product degradationMethod(@PathVariable("id")Long id,BlockException e){
        e.printStackTrace();
        return new Product(-1L,"控流了，你还在点尼玛呢，听不懂人话吗？？？？",200,new Store());
    }

}
